package com.bbva.kyfb.dao;

import static org.junit.Assert.assertTrue;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.transaction.annotation.Transactional;

import com.bbva.kyfb.kyfbdaofo.interfaces.SfoDao;
import com.bbva.kyfb.kyfbdtofo.entitys.SfoDTO;
import com.bbva.kyfb.kyfbjpatlbh.dominiotlbh.Ttlsbsfo;
import com.bbva.kyfb.kyfbjpatlbh.dominiotlbh.TtlsbsfoPK;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {
"classpath:META-INF/spring/applicationContext.xml" })
// @TransactionConfiguration(defaultRollback = false)
public class Select {

	@Autowired
	SfoDao ttlsbsfoDao;

	@Autowired
	@Qualifier("jdbcTemplateTLBH")
	JdbcTemplate jdbcTemplate;

	@Test
	public void typeTest() {
//		List<Ttlsbsfo> selectList = ttlsbsfoDao.selectList();
//		assertTrue(selectList.size() > 0);
	}

	@SuppressWarnings("unused")
	@Test
	public void changePrimaryKeyValueTest() {
		int queryForInt = jdbcTemplate
				.queryForInt("select count(*) from TLSB.TTLSBSFO");
		System.out.println(queryForInt);

		TtlsbsfoPK id = new TtlsbsfoPK(new BigDecimal("468898710394"),
				"hlgghic7", "TRA", "0023000120027999");

		Ttlsbsfo ttlsbsfo = new Ttlsbsfo();
		ttlsbsfo.setId(id);

		Ttlsbsfo ttlsbsfo5 = ttlsbsfoDao.select(id);

		delete(ttlsbsfo);

		// ttlsbsfo5.getId().setQnuNumitem(new BigDecimal("468898710396"));

		try {
			update(ttlsbsfo5);
		} catch (Exception e) {
			System.out.println(e.toString());
		}

		TtlsbsfoPK id2 = new TtlsbsfoPK(new BigDecimal("468898710396"),
				"hlgghic7", "TRA", "0023000120027999");

		Ttlsbsfo ttlsbsfo3 = new Ttlsbsfo();
		ttlsbsfo3.setId(id2);

		Ttlsbsfo ttlsbsfo4 = ttlsbsfoDao.select(id2);

		queryForInt = jdbcTemplate
				.queryForInt("select count(*) from TLSB.TTLSBSFO");
		System.out.println(queryForInt);

	}

	@Transactional("transactionManagerTLBH")
	private void delete(Ttlsbsfo ttlsbsfo) {
		// TODO Auto-generated method stub
		ttlsbsfoDao.delete(ttlsbsfo);
		ttlsbsfoDao.flush();
	}

	@Transactional("transactionManagerTLBH")
	private void update(Ttlsbsfo ttlsbsfo2) {

		// Ttlsbsfo ttlsbsfo3 = ttlsbsfoDao.read(ttlsbsfo2);

		// Ttlsbsfo cloned = ttlsbsfo3.clone();

		// Ttlsbsfo copi = new Ttlsbsfo();

		// ttlsbsfo3.c

		// ttlsbsfoDao.delete(ttlsbsfo2);

		// ttlsbsfo2.setCodEstadods("AA");

		TtlsbsfoPK id2 = new TtlsbsfoPK(new BigDecimal("468898710396"),
				"hlgghic7", "TRA", "0023000120027999");

		ttlsbsfo2.setId(id2);

		ttlsbsfoDao.insert(ttlsbsfo2);
		ttlsbsfoDao.flush();
	}

	@Test
	public void insertarTest() {
		/*
		 * insert into TLSB.TTLSBSFO ( QNU_NUMITEM, 468898710394 COD_IDORDEN,
		 * hlgghic7 COD_CLASEORD, TRA COD_CLIECASH, 0023000120027999
		 * 
		 * 
		 * 
		 * TIM_FICHERO, 2013-09-11 13:25:10.325 COD_BANCOPR, 1 COD_PRODSERV,
		 * 1380 COD_SUBPRODU, 0 COD_COMREFEX, ".TRA        " DES_ASUNACCE,
		 * "ES0301823999380201500505                " COD_ESTADODS, FF DES_PATH,
		 * "/buzones/0023/9/99/0023000120027999/                                                                "
		 * QNU_PESOFIR, 0.00 AUD_USUARIO, xatlsb1d AUD_FMODIFIC, 2013-09-11
		 * XTI_MULTIREM, N IMP_MAXREG, 108.78, 8117 COD_SERCRUBD) values
		 * (468898710394, '2013-09-11 13:25:10.325', 'hlgghic7', 'TRA',
		 * '0023000120027999', 1, 1380, 0, '.TRA ', 'ES0301823999380201500505 ',
		 * 'FF', '/buzones/0023/9/99/0023000120027999/ ', 0.00, 'xatlsb1d',
		 * '2013-09-11', 'N', 108.78, 8117);
		 */

		int queryForInt = jdbcTemplate
				.queryForInt("select count(*) from TLSB.TTLSBSFO");
		System.out.println(queryForInt);

		insetando();

		queryForInt = jdbcTemplate
				.queryForInt("select count(*) from TLSB.TTLSBSFO");
		System.out.println(queryForInt);

	}

	@Transactional("transactionManagerTLBH")
	private void insetando() {
		Ttlsbsfo item = new Ttlsbsfo();
		item.setTimFichero(new Date());
		item.setCodBancopr((short) 1);
		item.setCodProdserv((short) 1380);
		item.setCodSubprodu((short) 0);
		item.setCodComrefex(".TRA        ");
		item.setDesAsunacce("ES0301823999380201500505                ");
		item.setCodEstadods("FF");
		item.setDesPath("/buzones/0023/9/99/0023000120027999/                                                                ");
		item.setQnuPesofir(new BigDecimal(0.00));
		item.setAudUsuario("xatlsb1d");
		item.setAudFmodific(new Date());
		item.setXtiMultirem('N');
		item.setImpMaxreg(new BigDecimal(108.78));
		item.setCodSercrubd((short) 8117);

		TtlsbsfoPK id = new TtlsbsfoPK(new BigDecimal("468898710396"),
				"hlgghic6", "TRA", "0023000120027996");

		item.setId(id);
		ttlsbsfoDao.insert(item);
		ttlsbsfoDao.flush();
	}

	@SuppressWarnings("unused")
	@Test
	public void test() {
		assertTrue(ttlsbsfoDao != null);

		// int queryForInt =
		// jdbcTemplate.queryForInt("select count(*) from TLSB.TTLSBPPA");

		Ttlsbsfo ttlsbsfo = new Ttlsbsfo();
		TtlsbsfoPK id = new TtlsbsfoPK(new BigDecimal("468898710394"), null,
				"TRA", null);
		ttlsbsfo.setId(id);

		// public TtlsbsfoPK(BigDecimal qnuNumitem, String codIdorden, String
		// codClaseord, String codCliecash) {
		// ttlsbsfo.
		// S.COD_CLASEORD = 'TRA' and S.QNU_NUMITEM=468898710394

		List<SfoDTO> selectFromSFOandCAB = ttlsbsfoDao.selectSFOandCAFfromSFOandCABbySFOpk(id);

		// List<Map<String, Object>> lista =
		// jdbcTemplate.queryForList("SELECT * FROM TLSB.TTLSBSFO " +
		// "WHERE COD_CLASEORD = 'TRA' and QNU_NUMITEM=468898710394");

		// List<Ttlsbsfo> readList = ttlsbsfoDao.readList();

		// System.out.println(readList.size());

		// assertTrue(readList.size()>0);
	}

}
